package com.byedu.course.controller;

import com.byedu.course.util.ControllerUtil;
import com.byedu.course.util.FocusMailUtil;
import com.byedu.course.util.UserExportExcelUtil;
import com.byedu.domain.focus.*;
import com.byedu.domain.focus.vo.*;
import com.byedu.domain.focus.vo.enumvos.FocusDepartmentTypeVO;
import com.byedu.domain.focus.vo.enumvos.FocusExecuteTypeEnum;
import com.byedu.domain.focus.vo.enumvos.FocusStatusTypeVO;
import com.byedu.domain.focus.vo.enumvos.FocusdepaTypeEnum;
import com.byedu.focus.*;
import com.byedu.util.CommonUtil;
import com.byedu.util.FileUploadUtil;
import com.byedu.util.yunpian.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Luo on 2017-06-28.
 */
@Controller
@RequestMapping("/source")
public class AdminSourceController {


    @Autowired
    private FocusProduceService focusProduceService;
    @Autowired
    private FocusKpiService focusKpiService;
    @Autowired
    private FocusLogService focusLogService;
    @Autowired
    private FocusUserService focusUserService;
    @Autowired
    private FocusProjectService focusProjectService;
    @Autowired
    private FocusWorkorderService focusWorkorderService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String m = request.getParameter("m");
        FocusUserVO adminUser = ControllerUtil.checkuser(request, response);
        ControllerUtil.saveuser(request, focusProjectService, focusUserService, adminUser);

        if(StringUtils.isEmpty(m)){
            m = "source";
        }

//        request.setAttribute("m", m);
//        request.setAttribute("firstsnum", 0);
//        request.setAttribute("secondnum", 0);
//        request.setAttribute("thridnum", 0);
//        request.setAttribute("fourthnum", 0);
//        return "index";
        request.setAttribute("m", m);
        Calendar a=Calendar.getInstance();
        List<FocusKpiVO> kpiList= null;
        FocusKpiDO focusKpiDO = null;
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_1");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_1");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);

        request.setAttribute("firstsnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_2");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_2");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("secondnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_3");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_3");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("thridnum",focusKpiDO.getKipnum());
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
            kpiList  = focusKpiService.getFocusKpiDOByFocusKpiDO(null, a.get(Calendar.YEAR)+"_4");
        }else {
            kpiList = focusKpiService.getFocusKpiDOByFocusKpiDO(adminUser.getId(), a.get(Calendar.YEAR)+"_4");
        }
        focusKpiDO = focusKpiService.getkpiListSum(kpiList);
        request.setAttribute("fourthnum",focusKpiDO.getKipnum());
        return "index";
    }


    @RequestMapping("/workorder")
    public String workorder(HttpServletRequest req,HttpServletResponse resp) throws IOException{
        FocusUserVO adminUser = ControllerUtil.checkuser(req, resp);
        String m = req.getParameter("m");

        FocusWorkorderDO searchWork = new FocusWorkorderDO();
        String project = req.getParameter("project");
        String dapart = req.getParameter("dapart");
        String key = req.getParameter("key");

        if(!StringUtil.isNullOrEmpty(key)){
            key = new String(key.getBytes("iso8859-1"), "utf-8");
        }
        searchWork.setName(key);
        searchWork.setProjectid(CommonUtil.StringToInt(project));
        searchWork.setDepart(CommonUtil.StringToInt(dapart));
        System.out.println("searchWork:" + searchWork.toString());
        List<FocusDepartmentTypeVO> dapartList =null ;
        dapartList = focusWorkorderService.getFocusDepartmentTypeVOList();
//        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()) {
//
//             dapartList = focusWorkorderService.getFocusDepartmentTypeVOList();
//
//        }else{
//            dapartList = focusWorkorderService.getFocusDepartmentTypeVOList();
//        }
        req.setAttribute("dapartList", dapartList);


        FocusProjectDO focusProjectDO = new FocusProjectDO();

        List<FocusProjectDO> projectList=null;
        projectList = focusProjectService.getWorkerFocusProjectByFocusProjectDO(focusProjectDO);
//        if(adminUser.getPermission().getCode() != FocusdepaTypeEnum.AE.getCode()){
//            projectList = focusProjectService.getWorkerFocusProjectByFocusProjectDO(focusProjectDO);
//        }else{
//            focusProjectDO.setAeid(adminUser.getId());
//            projectList = focusProjectService.getWorkerFocusProjectByFocusProjectDO(focusProjectDO);
//        }
        req.setAttribute("projectList", projectList);

        if(m.equals("list")) {

            List<FocusProjectVO> focusProjectVOList = focusProjectService.getFocusProjectVOList();
            List<FocusUserVO> focususerVOList = focusUserService.getFocusUserList();
            List<FocusWorkorderVO> workorderList = focusWorkorderService.getFocusWorkorderdoVOList(focusProjectVOList, focususerVOList,searchWork);
//            for (FocusWorkorderVO focusWorkorderVO : workorderList) {
//                System.out.println("focusWorkorderVO:" + focusWorkorderVO.toString());
//                System.out.println("part:" + focusWorkorderVO.getDepart());
//            }
            req.setAttribute("workorderList", workorderList);
            return "source/workorderList";
        }
        if(m.equals("add")){
            //获取我的项目
            return ControllerUtil.toHtml(adminUser,"source/addworkorder",req,resp);
        }
        if(m.equals("edit")){
            String id = req.getParameter("id");
            FocusWorkorderDO workorderEntity = focusWorkorderService.getFocusWorkorderDObyId(id);
            req.setAttribute("workorderEntity", workorderEntity);
            return ControllerUtil.toHtml(adminUser,"source/addworkorder",req,resp);
        }


        return "source/workorderList";
    }


    @ResponseBody
    @RequestMapping("/addworker")
    public BaReSult addworker(FocusWorkorderDO focusWorkorderDO,
                           HttpServletRequest req,HttpServletResponse resp) throws IOException {
        FocusUserVO adminUser = ControllerUtil.checkuser(req, resp);
        String m = req.getParameter("m");
        System.out.println(focusWorkorderDO.toString());
        int retInt = 0;

//        System.out.println("sendUser:" + sendUser.toString());
        if (m.equals("doadd")) {
            if (adminUser.getPermission().getCode() <= FocusdepaTypeEnum.AE.getCode()) {
                focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(), "添加工单", focusWorkorderDO.toString()));
                retInt = focusWorkorderService.addFocusWorkorderDO(focusWorkorderDO);

                FocusUserDO checkUser = new FocusUserDO();
                checkUser.setDepart(focusWorkorderDO.getDepart());
                checkUser.setId(focusWorkorderDO.getUserid());
                FocusUserVO sendUser =  focusUserService.getsendFocusUser(checkUser);
                FocusMailUtil.mailsendworker(sendUser);
            }
        }

        if (m.equals("doedit")) {
            if (adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()) {

                focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(), "修改工单", focusWorkorderDO.toString()));
                retInt = focusWorkorderService.updateFocusWorkorderDObyId(focusWorkorderDO);

            }
        }

        if (m.equals("distrb")) {
            focusWorkorderDO.setStatus("2");
            focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(), "分配工单", focusWorkorderDO.toString()));
            retInt = focusWorkorderService.updateFocusWorkorderDObyId(focusWorkorderDO);
            FocusUserDO checkUser = new FocusUserDO();
            checkUser.setDepart(focusWorkorderDO.getDepart());
            checkUser.setId(focusWorkorderDO.getUserid());
            System.out.println(checkUser.toString());
            FocusUserVO sendUser =  focusUserService.getsendFocusUser(checkUser);
            System.out.println(sendUser.toString());
            FocusMailUtil.mailsendworker(sendUser);
        }

        if(retInt > 0){
            return new BaReSult(true);
        }else{
            return new BaReSult("请联系管理员");
        }
    }

    @ResponseBody
    @RequestMapping("/cancelworkorder")
    public BaReSult cancelworkorder(FocusWorkorderDO focusWorkorderDO,
                             HttpServletRequest req,HttpServletResponse resp) throws IOException {
        FocusUserVO adminUser = ControllerUtil.checkuser(req, resp);
        String m = req.getParameter("m");
        focusWorkorderDO.setStatus("4");
        int retInt = 0;
        System.out.println("focusWorkorderDO:" + focusWorkorderDO.toString());
        if(adminUser.getPermission().getCode() <= FocusdepaTypeEnum.AE.getCode()){
                focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"取消工单",focusWorkorderDO.toString()));
                retInt = focusWorkorderService.updateFocusWorkorderDObyId(focusWorkorderDO);
        }

        if(retInt > 0){
            return new BaReSult(true);
        }else{
            return new BaReSult("请联系管理员");
        }
    }

    @RequestMapping("/distribut")
    public String distribut(HttpServletRequest req,HttpServletResponse resp) throws IOException{
        FocusUserVO adminUser = ControllerUtil.checkuser(req, resp);
        String m = req.getParameter("m");
        FocusWorkorderDO searchWork = new FocusWorkorderDO();
        String project = req.getParameter("project");
        String dapart = req.getParameter("dapart");
        String key = req.getParameter("key");
        if(!StringUtil.isNullOrEmpty(key)){
            key = new String(key.getBytes("iso8859-1"), "utf-8");
        }
        searchWork.setName(key);
        searchWork.setProjectid(CommonUtil.StringToInt(project));
        searchWork.setDepart(CommonUtil.StringToInt(dapart));

//        List<FocusDepartmentTypeVO> dapartList =null ;
//        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()) {
//
//            dapartList = focusWorkorderService.getFocusDepartmentTypeVOList();
//
//        }else{
//            dapartList = focusWorkorderService.getFocusDepartmentTypeVOList();
//        }
//        req.setAttribute("dapartList", dapartList);


        FocusProjectDO focusProjectDO = new FocusProjectDO();

        List<FocusProjectDO> projectList=null;
        projectList = focusProjectService.getWorkerFocusProjectByFocusProjectDO(focusProjectDO);
//        if(adminUser.getExecute().getCode() == FocusExecuteTypeEnum.Manage.getCode()){
//
//            projectList = focusProjectService.getWorkerFocusProjectByFocusProjectDO(focusProjectDO);
//        }else{
//            focusProjectDO.setAeid(adminUser.getId());
//            projectList = focusProjectService.getWorkerFocusProjectByFocusProjectDO(focusProjectDO);
//        }
        req.setAttribute("projectList", projectList);

        if(m.equals("list")) {

            List<FocusProjectVO> focusProjectVOList = focusProjectService.getFocusProjectVOList();
            List<FocusUserVO> focususerVOList = focusUserService.getFocusUserList();
            List<FocusWorkorderVO> workorderList=null;
            searchWork.setDepart(adminUser.getDepart().getCode());
            searchWork.setUserid(adminUser.getId());

            if(FocusExecuteTypeEnum.Manage.getCode() == adminUser.getExecute().getCode()) {

                workorderList = focusWorkorderService.getFocusManageWorkorderVOList(focusProjectVOList, focususerVOList, searchWork);
            }else{

                workorderList = focusWorkorderService.getFocusExcutehWorkorderVOList(focusProjectVOList, focususerVOList, searchWork);
            }
//            for (FocusWorkorderVO focusWorkorderVO : focusWorkorderVOList) {
//                System.out.println("focusWorkorderVO:" + focusWorkorderVO.toString());
//            }
            req.setAttribute("workorderList", workorderList);
            return "source/distributList";
        }
        if(m.equals("edit")){
            String id = req.getParameter("id");
            FocusWorkorderDO workorderEntity = focusWorkorderService.getFocusWorkorderDObyId(id);
            req.setAttribute("workorderEntity", workorderEntity);
            System.out.println("workorderEntity:"+workorderEntity);
            //部门名单
            List<FocusUserVO> userList = focusUserService.getFocusUserbydapartList(adminUser.getDepart().getCode());
            req.setAttribute("userList", userList);
            return ControllerUtil.toHtml(adminUser,"source/adddistributworkorder",req,resp);
        }

        return "source/distributList";
    }

    @ResponseBody
    @RequestMapping("/adddistribut")
    public BaReSult adddistribut(FocusWorkorderDO focusWorkorderDO,
                                    HttpServletRequest req,HttpServletResponse resp) throws IOException {
        FocusUserVO adminUser = ControllerUtil.checkuser(req, resp);


        String id = req.getParameter("id");
        String statustr = req.getParameter("statustr");
        FocusWorkorderDO workorderEntity = new FocusWorkorderDO();
        workorderEntity.setId(id);
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        workorderEntity.setFinishtime(dateString);
        workorderEntity.setStatus("3");
        workorderEntity.setStatustr(statustr);
        int retInt = focusWorkorderService.updateFocusWorkorderDObyId(workorderEntity);
        focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"完成工单",focusWorkorderDO.toString()));
        if(retInt > 0){

            return new BaReSult(true);
        }else{
            return new BaReSult("请联系管理员");
        }
    }




    @RequestMapping("/mine")
    public String mine(HttpServletRequest req,HttpServletResponse resp) throws IOException{

        FocusUserVO adminUser = ControllerUtil.checkuser(req, resp);
        String m = req.getParameter("m");
        FocusWorkorderDO searchWork = new FocusWorkorderDO();
        String dapart = req.getParameter("dapart");
        String jidu = req.getParameter("jidu");
        String key = req.getParameter("key");
        String userid = req.getParameter("userid");

        if(!StringUtil.isNullOrEmpty(key)){
            key = new String(key.getBytes("iso8859-1"), "utf-8");
        }
        searchWork.setName(key);
        searchWork.setDepart(CommonUtil.StringToInt(dapart));
        searchWork.setUserid(userid);

        List<FocusDepartmentTypeVO> dapartList =null ;
        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()) {

            dapartList = focusWorkorderService.getFocusDepartmentTypeVOList();

        }else{
            dapartList = focusWorkorderService.getFocusDepartmentTypeVOList();
        }

        FocusProjectDO focusProjectDO = new FocusProjectDO();

        List<FocusUserVO> focususerVOList = focusUserService.getFocusUserList();
        List<FocusProjectVO> focusProjectVOList = null ;
        List<FocusWorkorderVO> workorderList =null;

        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){

            focusProjectVOList = focusProjectService.getFocusProjectVOList();

        }else{
            searchWork.setDepart(adminUser.getDepart().getCode());
            if(FocusExecuteTypeEnum.Manage.getCode() == adminUser.getExecute().getCode()){
                focusProjectDO.setAeid(adminUser.getId());

                focusProjectVOList = focusProjectService.getFocusProjectVOList();

            }else{
                searchWork.setUserid(adminUser.getId());
                focusProjectDO.setAeid(adminUser.getId());

                focusProjectVOList = focusProjectService.getFocusProjectVOList();

            }
        }
        workorderList = focusWorkorderService.getFocusmineWorkorderVOList(focusProjectVOList, focususerVOList, searchWork,CommonUtil.StringToInt(jidu));
        req.setAttribute("userList", focususerVOList);
        req.setAttribute("dapartList", dapartList);
        req.setAttribute("downexcel", UserExportExcelUtil.workorderExcelexpot(workorderList,adminUser));
        req.setAttribute("workorderList", workorderList);
        return "source/mineList";
    }



    @RequestMapping("/finish")
    public String finish(HttpServletRequest req,HttpServletResponse resp) throws IOException{
        FocusUserVO adminUser = ControllerUtil.checkuser(req, resp);
        String m = req.getParameter("m");
        FocusWorkorderDO searchWork = new FocusWorkorderDO();
        String project = req.getParameter("project");
        String dapart = req.getParameter("dapart");
        String key = req.getParameter("key");

        if(!StringUtil.isNullOrEmpty(key)){
            key = new String(key.getBytes("iso8859-1"), "utf-8");
        }
        searchWork.setName(key);
        searchWork.setProjectid(CommonUtil.StringToInt(project));
        searchWork.setDepart(CommonUtil.StringToInt(dapart));

        List<FocusDepartmentTypeVO> dapartList =null ;
        dapartList = focusWorkorderService.getFocusDepartmentTypeVOList();
//        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
//
//            dapartList = focusWorkorderService.getFocusDepartmentTypeVOList();
//
//        }else{
//            dapartList = focusWorkorderService.getFocusDepartmentTypeVOList();
//        }
        req.setAttribute("dapartList", dapartList);


        FocusProjectDO focusProjectDO = new FocusProjectDO();

        List<FocusProjectDO> projectList=null;
        projectList = focusProjectService.getWorkerFocusProjectByFocusProjectDO(focusProjectDO);
//        if(adminUser.getPermission().getCode() == FocusdepaTypeEnum.BOSS.getCode()){
//            projectList = focusProjectService.getWorkerFocusProjectByFocusProjectDO(focusProjectDO);
//        }else{
//            focusProjectDO.setAeid(adminUser.getId());
//            projectList = focusProjectService.getWorkerFocusProjectByFocusProjectDO(focusProjectDO);
//        }
        req.setAttribute("projectList", projectList);

        if(m.equals("list")) {

            List<FocusProjectVO> focusProjectVOList = focusProjectService.getFocusProjectVOList();
            List<FocusUserVO> focususerVOList = focusUserService.getFocusUserList();
            List<FocusWorkorderVO> workorderList = focusWorkorderService.getFocusFinishWorkorderVOList(focusProjectVOList, focususerVOList,searchWork);
//            for (FocusWorkorderVO focusWorkorderVO : focusWorkorderVOList) {
//                System.out.println("focusWorkorderVO:" + focusWorkorderVO.toString());
//            }
            req.setAttribute("workorderList", workorderList);
            return "source/finishList";
        }
        if(m.equals("add")){
            //获取我的项目
            return ControllerUtil.toHtml(adminUser,"source/addworkorder",req,resp);
        }
        if(m.equals("edit")){
            String id = req.getParameter("id");
            FocusWorkorderDO workorderEntity = focusWorkorderService.getFocusWorkorderDObyId(id);
            req.setAttribute("workorderEntity", workorderEntity);
            System.out.println("workorderEntity:"+workorderEntity);
//            return "source/addworkorder";
            return ControllerUtil.toHtml(adminUser,"source/addworkorder",req,resp);
        }


        return "source/finishList";
    }




    @RequestMapping("/projectcheck")
    public String  project(HttpServletRequest req,HttpServletResponse resp) throws IOException {

        String m = req.getParameter("m");
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
        ControllerUtil.saveuser(req, focusProjectService, focusUserService, adminUser);
        List<FocusProduceDO>  produceList  = focusProduceService.getFocusProduceDOList();

        List<JiduVO> jiduList  = focusKpiService.getjifuList();
        req.setAttribute("jiduList",jiduList);

        List<FocusUserVO> aes  = focusUserService.getFocusUserListbypermission(FocusdepaTypeEnum.AE.getCode());
//        List<FocusCategoryTypeVO> focusCategoryTypeVOList = focusProjectService.getFocusCategoryTypeVOList();
        List<FocusStatusTypeVO> focusStatusTypeVOList = focusProjectService.getFocusStatusTypeVOList();

        req.setAttribute("produceList",produceList);
        req.setAttribute("aes",aes);
//        req.setAttribute("categorys",focusCategoryTypeVOList);
        req.setAttribute("status",focusStatusTypeVOList);

//        req.setAttribute("jiduList",jiduList);

            String id = req.getParameter("id");
            FocusProjectVO projectEntity = focusProjectService .getFocusProjectbyId(id);
            req.setAttribute("projectEntity",projectEntity);
            return ControllerUtil.toHtml(adminUser,"source/checkproject",req,resp);
    }




    @ResponseBody
    @RequestMapping("/upload")
    public BaReSult upload(HttpServletResponse resp, HttpServletRequest req
    ) throws IOException {
//        String fileName = myfile.getOriginalFilename(); //获取文件名
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
//        System.out.print("adminUser:"+adminUser.toString());
        //先创建本地文件夹
        String  img = new FileUploadUtil().saveFocusOneFile(req);
//        System.out.print("img:"+img);
        if(StringUtil.isNullOrEmpty(img)){
            return new BaReSult(false,"文件上传失败");
        }else{
            focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"上传报表",img));
            return new BaReSult(true,img);
        }
    }


    @ResponseBody
    @RequestMapping("/upload1")
    public BaReSult upload1(HttpServletResponse resp, HttpServletRequest req
    ) throws IOException {
//        String fileName = myfile.getOriginalFilename(); //获取文件名
        FocusUserVO adminUser = ControllerUtil.checkuser(req,resp);
//        System.out.print("adminUser:"+adminUser.toString());
        //先创建本地文件夹
        String  img = new FileUploadUtil().saveFocusOneFile(req);
//        System.out.print("img:"+img);
        if(StringUtil.isNullOrEmpty(img)){
            return new BaReSult(false,"文件上传失败");
        }else{
            focusLogService.addFocusLogDO(new FocusLogDO(adminUser.getId(),"上传报表",img));
            return new BaReSult(true,img);
        }
    }
}
